package Leetcode;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class searchMatrix {
//    public boolean searchMatrix(int[][] matrix, int target) {
//        for (int i = 0; i < matrix.length; i++) {
//            for (int j = 0; j < matrix[0].length; j++) {
//                if (matrix[i][j] == target){
//                    return true;
//                }
//            }
//        }
//        return false;
//    }
    public boolean searchMatrix(int[][] matrix, int target) {
        for (int[] nums : matrix) {
            int index = search(nums, target);
            if (index >= 0){
                return true;
            }
        }
        return false;
    }
    public int search(int[] nums, int target){
        int low = 0, high = nums.length - 1;
        while (low <= high){
            int mid = (high - low) / 2 + low;
            int num = nums[mid];
            if (num == target){
                return mid;
            } else if (num > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}
